//
//  NEDInputTextView.m
//  AddressBookCloud
//
//  Created by 林宁宁 on 2020/5/12.
//  Copyright © 2020 林宁宁. All rights reserved.
//

#import "NEDInputTextView.h"
#import "UIView+NEDInputAccessory.h"
#import "UIFont+NED.h"
#import "NEDDefines.h"
#import "NEDConstant.h"
#import "UIColor+NEDStyle.h"
#import "NSMutableAttributedString+NED.h"
#import "UIView+NED.h"

@interface NEDInputTextView()<UITextViewDelegate>

@property (retain, nonatomic) UIView * leftContentView;
@property (retain, nonatomic) UIView * rightContentView;

@property (retain, nonatomic) UITextView * textView;

@end

@implementation NEDInputTextView


-(void)ned_commonInit{
    [super ned_commonInit];
    
    WS(weakSelf);
    
    self.backgroundColor = [UIColor ned_background];
    
    self.isVerticalCenter = YES;
    
    self.textView = [UITextView new];
    self.textView.textColor = [UIColor ned_h1];
    self.textView.textAlignment = NSTextAlignmentLeft;
    self.textView.font = [UIFont ned_pfOfSize:16];
    self.textView.backgroundColor = [UIColor clearColor];
    self.textView.delegate = self;
    [self showAccessoryDown:^{
        [weakSelf resignFirstResponder];
    }];
    [self addSubview:self.textView];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo(self);
    }];
}


- (void)addLeftViewShowSize:(CGSize)size image:(UIImage *)image{
    [self addHeadViewShowSize:size attributeTitle:[NSMutableAttributedString ned_initWithStr:@"" customBlock:^(NSMutableAttributedString *att) {
        [att ned_attachImage:image];
    }] isLeft:YES action:nil];
}
- (void)addRightViewShowSize:(CGSize)size image:(UIImage *)image{
    [self addRightViewShowSize:size image:image action:nil];
}
- (void)addRightViewShowSize:(CGSize)size image:(UIImage *)image action:(NEDVoidBlock)action{
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = image;
    if(action){
        [imageView addTapClick:action];
    }
    [self addHeadViewShowSize:size customView:imageView isLeft:NO];
}
- (void)addLeftViewShowSize:(CGSize)size title:(NSString *)title{
    [self addHeadViewShowSize:size attributeTitle:[NSMutableAttributedString ned_initWithStr:title customBlock:^(NSMutableAttributedString *att) {
        [att ned_font:[UIFont ned_pflOfSize:16]];
        [att ned_color:[UIColor ned_h1]];
    }] isLeft:YES action:nil];
}
- (void)addRightViewShowSize:(CGSize)size title:(NSString *)title{
    [self addHeadViewShowSize:size attributeTitle:[NSMutableAttributedString ned_initWithStr:title customBlock:^(NSMutableAttributedString *att) {
        [att ned_font:[UIFont ned_pflOfSize:16]];
        [att ned_color:[UIColor ned_h1]];
    }] isLeft:NO action:nil];
}

- (void)addViewShowSize:(CGSize)size title:(NSString *)title image:(UIImage *)image left:(BOOL)isLeft{
    
    NSMutableAttributedString * attribute = [NSMutableAttributedString new];
    [attribute ned_attachImage:[[UIColor clearColor] ned_drawImageWithColor] imageSize:CGSizeMake(Adapted(6), Adapted(8))];
    [attribute ned_attachImage:image imageSize:CGSizeMake(25, 25)];
    [attribute ned_attachImage:[[UIColor clearColor] ned_drawImageWithColor] imageSize:CGSizeMake(Adapted(8), Adapted(8))];
    [attribute ned_appendStr:title block:^(NSMutableAttributedString *attStr, NSString *str) {
        [attStr ned_font:[UIFont ned_pfOfSize:16]];
        [attStr ned_color:[UIColor ned_h1]];
    }];
    [attribute ned_baseLineOffset:(25-16)*0.5 range:[attribute.string rangeOfString:title]];
    
    [self addHeadViewShowSize:size attributeTitle:attribute isLeft:isLeft action:nil];
}

- (void)addHeadViewShowSize:(CGSize)size attributeTitle:(NSAttributedString *)attribute isLeft:(BOOL)isLeft action:(NEDVoidBlock)action{
    UILabel * titleView = [UILabel new];
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.numberOfLines = 0;
    titleView.attributedText = attribute;
    if(action){
        [titleView addTapClick:action];
    }
    [self addHeadViewShowSize:size customView:titleView isLeft:isLeft];
}



- (void)addHeadViewShowSize:(CGSize)size customView:(UIView *)customView isLeft:(BOOL)isLeft{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        CGFloat space = (size.height - self.textView.font.pointSize)*0.5;
        self.textView.contentInset = UIEdgeInsetsMake(0, -5, 0, -15);
        self.textView.textContainerInset = UIEdgeInsetsMake(space, 0, 5, 15);
        
        if(customView){
            if(isLeft){
                [self cleanLeftView];
                self.leftContentView = customView;
                [self addSubview:self.leftContentView];
                [self.leftContentView mas_makeConstraints:^(MASConstraintMaker *make) {
//                    make.left.top.equalTo(self);
//                    make.size.mas_equalTo(size);
                    make.left.centerY.equalTo(self);
                    make.width.mas_equalTo(size.width);
                }];
                
                [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self).offset(size.width);
                }];
            }else{
                [self cleanRightView];
                self.rightContentView = customView;
                [self addSubview:self.rightContentView];
                [self.rightContentView mas_makeConstraints:^(MASConstraintMaker *make) {
//                    make.right.top.equalTo(self);
//                    make.size.mas_equalTo(size);
                    make.right.centerY.equalTo(self);
                    make.width.mas_equalTo(size.width);
                }];
                
                [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(self).offset(-size.width);
                }];
            }
        }
    });
}



- (void)cleanLeftView{
    [self.leftContentView removeFromSuperview];
    self.leftContentView = nil;
}
- (void)cleanRightView{
    [self.rightContentView removeFromSuperview];
    self.rightContentView = nil;
}

-(void)setText:(NSString *)text{
    _text = text;
    
    self.textView.attributedText = nil;
    self.textView.text = text;
    
    [self updateFrame];
}

-(void)setAttText:(NSAttributedString *)attText{
    
    self.textView.attributedText = attText;
    
    [self updateFrame];
}

- (void)updateFrame{
    
    if(self.isVerticalCenter){
        if([self.textView.text length]>0){
            //textView的contentSize属性
            CGSize contentSize = self.textView.contentSize;
            //textView的内边距属性
            UIEdgeInsets offset;
            CGSize newSize = contentSize;
            
            //如果文字内容高度没有超过textView的高度
            if(contentSize.height <= self.textView.frame.size.height){
                //textView的高度减去文字高度除以2就是Y方向的偏移量，也就是textView的上内边距
                CGFloat offsetY = (self.textView.frame.size.height - contentSize.height)/2;
                offset = UIEdgeInsetsMake(offsetY, 0, 0, 0);
            }else{
                //如果文字高度超出textView的高度
                newSize = self.textView.frame.size;
                offset = UIEdgeInsetsZero;
                CGFloat fontSize = 18;

               //通过一个while循环，设置textView的文字大小，使内容不超过整个textView的高度（这个根据需要可以自己设置）
                while (contentSize.height > self.textView.frame.size.height &&
                       fontSize > 0){
                    [self.textView setFont:[UIFont fontWithName:@"Helvetica Neue" size:fontSize--]];
                    contentSize = self.textView.contentSize;
                }
                newSize = contentSize;
            }
            
            //根据前面计算设置textView的ContentSize和Y方向偏移量
            [self.textView setContentSize:newSize];
            [self.textView setContentInset:offset];
        }
    }
}


- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if(self.beginEditBlock){
        return self.beginEditBlock(self);
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    if(self.endEditBlock){
        return self.endEditBlock(self);
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    if(self.didChangeBlock){
        return self.didChangeBlock(self);
    }
}

@end
